package com.umeox.watch.moto.launcher.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.umeox.watch.moto.launcher.R;
import com.umeox.watch.moto.launcher.bean.QuickSettingsBean;
import com.umeox.moto.watch.themes.ItemClickListener;
import com.umeox.watch.moto.launcher.event.OnItemLongClickListener;

import java.util.ArrayList;
import java.util.List;

public class QuickSettingsAdapter extends RecyclerView.Adapter {

    private OnItemLongClickListener mLongClickListener;

    public List<QuickSettingsBean> getQuickSettingsBeans() {
        return mQuickSettingsBeans;
    }

    private List<QuickSettingsBean> mQuickSettingsBeans = new ArrayList<>();
    private ItemClickListener mItemClickListener;

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_quick_settings, viewGroup, false);

        return new QuickSettingsViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        QuickSettingsViewHolder quickSettingsViewHolder = (QuickSettingsViewHolder) viewHolder;
        final int position = i;
        quickSettingsViewHolder.itemView.setOnClickListener(v -> {
            if (mItemClickListener != null) {
                mItemClickListener.onItemClicked(position);
            }
        });
        quickSettingsViewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mLongClickListener != null) {
                    mLongClickListener.onItemLongClicked(position);
                    return true;
                }
                return false;
            }
        });
        quickSettingsViewHolder.bindData(mQuickSettingsBeans.get(i));
    }

    @Override
    public int getItemCount() {
        return mQuickSettingsBeans.size();
    }


    public void setDataList(List<QuickSettingsBean> datalist) {
        mQuickSettingsBeans.clear();
        mQuickSettingsBeans.addAll(datalist);
        notifyDataSetChanged();
    }

    class QuickSettingsViewHolder extends RecyclerView.ViewHolder {
        private ImageView mIconIv;

        public QuickSettingsViewHolder(@NonNull View itemView) {
            super(itemView);
            mIconIv = itemView.findViewById(R.id.iv_quick_setting);

        }


        public void bindData(QuickSettingsBean quickSettingsBean) {
            mIconIv.setImageResource(quickSettingsBean.getIconResId());

        }
    }

    public void setItemClickListener(ItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener listener) {

        mLongClickListener = listener;
    }
}
